package au.com.zonski.gwlite.j2me.client;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class GWLiteMIDlet extends MIDlet 
{
	private GWLiteCanvas canvas;
	
	private static final String IMAGES_KEY 			= "images";
	private static final String IMAGE_KEY_PREFIX	= "image.";
	
	private static final String[] PROPERTIES = {
"15,15,18,1,10,10,/image/bullet.png",
"10,10,10,2,10,10,/image/wormtail.png",
"20,20,12,0,15,15,/image/star.png",
"30,30,17,0,20,20,1,2,3,4,5,6,7,8,7,6,5,4,3,2,1,0,9,10,11,12,13,14,15,16,15,14,13,12,11,10,9,/image/diamond.png",
"20,20,12,0,15,15,/image/square.png",
"20,20,12,0,15,15,/image/square2.png",
"15,15,1,0,10,10,/image/circle.png",
"15,15,9,1,10,10,/image/wormhead.png",
"30,30,9,1,20,20,/image/interceptor.png",
"30,30,12,0,20,20,/image/butterfly.png",
"30,30,7,-1,20,20,/image/blackhole.png",
"20,20,9,1,12,12,/image/player.png",
"10,10,12,0,8,8,/image/smallsquare.png",
"100,100,7,2,0,0,/image/bullet.x.png",
"15,15,7,2,0,0,/image/thrust.png",
"12,12,1,-1,0,0,/image/bombicon.png",
"12,12,1,-1,0,0,/image/playericon.png"
	};
	
	protected void destroyApp(boolean force) throws MIDletStateChangeException 
	{
		if( this.canvas != null )
		{
			this.canvas.stop();
		}
		this.canvas = null;
	}

	protected void pauseApp() 
	{
		if( this.canvas != null )
		{
			this.canvas.stop();
		}
	}

	protected void startApp() throws MIDletStateChangeException 
	{
		// loading canvas
		TextBox loadingScreen = new TextBox( "Loading", "Starting GridWars Lite...", 2048, TextField.UNEDITABLE );
		Display.getDisplay( this ).setCurrent( loadingScreen );
		
		try
		{
			if( this.canvas == null )
			{
				int numberOfImages = PROPERTIES.length;
				loadingScreen.insert( "\n#images:"+numberOfImages, loadingScreen.size() );
				int[][] spriteDetails = new int[ numberOfImages][];
				Image[] images = new Image[ numberOfImages ];
				for( int i=0; i<numberOfImages; i++ )
				{
					String imageString = PROPERTIES[ i ];
					loadingScreen.insert( "\nparsing image data "+imageString, loadingScreen.size() );
					int commaCount = 0;
					for( int j=imageString.length(); j>0; )
					{
						j--;
						if( imageString.charAt(j) == ',' )
						{
							commaCount++;
						}
					}
					spriteDetails[ i ] = new int[ commaCount ];
					int j = 0;
					int index = 0;
					int nextIndex;
					while( ( nextIndex = imageString.indexOf( ",", index ) ) >= 0 )
					{
						String s = imageString.substring( index, nextIndex );
						index = nextIndex + 1;
						int number = Integer.parseInt( s );
						spriteDetails[ i ][ j ] = number;
						j++;
					}
					String imageName = imageString.substring( index );
					loadingScreen.insert( "\ncreating image "+imageName, loadingScreen.size() );
					Image image = Image.createImage( imageName );
					if( image == null )
					{
						throw new Exception("unable to load image "+imageName );
					}
					images[ i ] = image; 
				}
				loadingScreen.insert( "\ncreating canvas...", loadingScreen.size() );
				this.canvas = new GWLiteCanvas( images, spriteDetails );
			}
			loadingScreen.insert( "\ninstalling background...", loadingScreen.size() );
			Image background = Image.createImage( "/image/grid.png" );
			this.canvas.buildBackground( background );
			loadingScreen.insert( "\nstarting canvas...", loadingScreen.size() );
			this.canvas.start();
			Display.getDisplay( this ).setCurrent( this.canvas );
		}
		catch( Exception ex )
		{
			ex.printStackTrace();
			loadingScreen.insert( "\nError : "+ex.toString(), loadingScreen.size() );
		}
	}
	
}
